package com.twang.stock.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.twang.stock.entity.*;
import com.twang.stock.entity.ex.KaiPanLaBanKuaiEx;
import com.twang.stock.entity.ex.StockWarningEventEx;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import com.twang.stock.mapper.CompanyStockMinutesMapper;
import com.twang.stock.mapper.KaiPanLaZhangTingInfoMapper;
import com.twang.stock.mapper.StockWarningEventMapper;
import com.twang.stock.dto.StockMinutesMessage;
import com.twang.stock.utils.date.HolidayCopy;
import com.twang.stock.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class StockWarningEventService {

    public static String StockWarningEvent_YU_JING = "StockWarningEvent_YU_JING_";
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private StockWarningEventMapper stockWarningEventMapper;

    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;

    @Resource
    private CompanyStockMinutesMapper companyStockMinutesMapper;

    @Resource
    private KaiPanLaZhangTingInfoMapper kaiPanLaZhangTingInfoMapper;

    /**
     * 查询预警信息
     */
    public List<StockWarningEventEx>  queryYuJing() {
        List<StockWarningEventEx> stockWarningEventExes = Lists.newArrayList();
        String key = StockWarningEvent_YU_JING + HolidayCopy.getRealToday();
        Object obj = redisUtil.get(key);
        if (null != obj) {
            List<StockWarningEventEx> stockWarningEventExes1 = JSONObject.parseArray(obj.toString(),StockWarningEventEx.class);
            return stockWarningEventExes1;
        } else {
            List<String> dates = companyStockMinutesMapper.queryDates();
            String date = dates.get(dates.size() - 2);
            StringBuffer datestr = new StringBuffer(date.substring(0, 4)).append("-").append(date.substring(4, 6)).append("-").append(date.substring(6, 8)).append(" 23:59:59");
            List<StockWarningEvent> stockWarningEventList = stockWarningEventMapper.yuJingList(datestr.toString());
            //去重
            List<StockWarningEvent> stockWarningEventListNew=Lists.newArrayList();
            List<String> nameList=Lists.newArrayList();
            for(StockWarningEvent stockWarningEvent:stockWarningEventList){
                if(!nameList.contains(stockWarningEvent.getName())){
                    stockWarningEventListNew.add(stockWarningEvent);
                    nameList.add(stockWarningEvent.getName());
                }
            }
            //数据
            if (CollectionUtils.isNotEmpty(stockWarningEventListNew)) {
                stockWarningEventListNew.stream().forEach(item -> {
                    try {
                        查询预警信息(stockWarningEventExes, item);
                    } catch (Exception e) {
                        log.error(">>>>>>>>> 查询预警信息:{}", e);
                    }
                });

            }
            redisUtil.set(key, JSONObject.toJSONString(stockWarningEventExes));
        }
        return stockWarningEventExes;
    }


    /**
     * 板块预警信息
     * @return
     */
    public List<KaiPanLaBanKuaiEx>  queryYuJingBanKuai() {
        String key=StockMinutesMessage.TYPE_BAN_KUAI_JIN_E+"_"+HolidayCopy.getRealToday();
        List<KaiPanLaBanKuaiEx> kaiPanLaBanKuais = Lists.newArrayList();
        Object obj = redisUtil.get(key);
        if (null != obj) {
            List<KaiPanLaBanKuaiEx> kaiPanLaBanKuaiList =JSONObject.parseArray(obj.toString(),KaiPanLaBanKuaiEx.class);
            return kaiPanLaBanKuaiList;
        }
        return kaiPanLaBanKuais;
    }

    private void 查询预警信息(List<StockWarningEventEx> stockWarningEventExes, StockWarningEvent item) {
        StockWarningEventEx stockWarningEventEx = new StockWarningEventEx();
        //查询公司
        CompanyCodeInfo param = new CompanyCodeInfo();
        param.setShortName(item.getName());
        CompanyCodeInfo companyCodeInfo = companyCodeInfoMapper.selectOne(param);
        stockWarningEventEx.setName(companyCodeInfo.getShortName());
        stockWarningEventEx.setMessage(item.getMessage());
        stockWarningEventEx.setWarnTime(HolidayCopy.getHHmmssByDate(item.getWarnTime()));
        if(StringUtils.isNotEmpty(companyCodeInfo.getTrendTypeName())){
            stockWarningEventEx.setWarnMessage(companyCodeInfo.getTrendTypeName());
        }else{
            stockWarningEventEx.setWarnMessage("-");
        }
        //查询minnutes
        CompanyStockMinutes companyStockMinutes = companyStockMinutesMapper.queryByCode(companyCodeInfo.getCompanyCode());
        if (null != companyStockMinutes) {
            stockWarningEventEx.setVol(companyStockMinutes.getTradeVol());
            stockWarningEventEx.setZhangDie(companyStockMinutes.getZhangDie());
            stockWarningEventEx.setCode(companyStockMinutes.getCompanyCode().replace("sh", "").replace("sz", ""));
        }else{
            stockWarningEventEx.setVol(BigDecimal.ZERO);
            stockWarningEventEx.setZhangDie(BigDecimal.ZERO);
            stockWarningEventEx.setCode("-");
        }
        //查询概念
        KaiPanLaZhangTingInfo kaiPanLaZhangTingInfo = kaiPanLaZhangTingInfoMapper.queyByCode(companyStockMinutes.getCompanyCode().replace("sh", "").replace("sz", ""));
        if (null != kaiPanLaZhangTingInfo) {
            stockWarningEventEx.setBk(kaiPanLaZhangTingInfo.getZyglbk());
            stockWarningEventEx.setZhangTingId(kaiPanLaZhangTingInfo.getId());
        }else{
            stockWarningEventEx.setBk("-");
            stockWarningEventEx.setZhangTingId(0L);
        }
        stockWarningEventExes.add(stockWarningEventEx);
    }
}
